/*
 *   Copyright (C) 2019 All rights reserved.
 *
 *   FileName      ：util.c
 *   Author        ：yuefei.Chen
 *   Email         ：yfei777@outlook.com
 *   Date          ：2019年03月21日
 *   Description   ：
 */
#include "util.h"
#include <unistd.h>

void write_login_info(char *userName) {
				struct logininfo *linfo;
				FILE *fp;
				time_t t;
				linfo = (struct logininfo *) malloc(sizeof(struct logininfo));
				memset(linfo, 0, sizeof(*linfo));
				strcpy(linfo->userName, userName);
				time(&t);
				strftime(linfo->loginTime, sizeof(linfo->loginTime), "%Y/%m/%d %X",
												localtime(&t));
				if ((fp = fopen("logininfo.txt", "wb")) == NULL) {
								return;
				}
				fwrite(linfo, sizeof(struct logininfo), 1, fp);
				free(linfo);
				fclose(fp);
}


struct logininfo *read_login_info() {
				struct logininfo *linfo;
				FILE *fp;
				if ((fp = fopen("logininfo.txt", "rb")) == NULL) {
								return NULL;
				}
				linfo = (struct logininfo *) malloc(sizeof(struct logininfo));
				fread(linfo, sizeof(struct logininfo), 1, fp);
				fclose(fp);
				return linfo;
}

void get_current_time(char time_str[], int n) {
				time_t t;
				time(&t);
				strftime(time_str, n, "%Y/%m/%d %X", localtime(&t));
}

void sys_info(long *uptime,long *totalram) {
				struct sysinfo info;
				if (!sysinfo(&info)) {
								*uptime = info.uptime;
								*totalram=info.totalram;
				};
}

void random_key(char str[], int num) {
				int i;
				srand(time(NULL));
				for (i = 0; i < num-1; i++) {
								str[i] = rand() % (0x7a - 0x61) + 0x61;
				}
				str[i+1]='\0';
}

void get_cpu_occupy(struct cpu_occupy *cpu_opy){

				FILE *fd;
				char buf[256]={0};
				struct cpu_occupy *cpup;
				cpup=cpu_opy;
				fd=fopen("/proc/stat","r");
				fgets(buf,sizeof(buf),fd);
				sscanf(buf,"%s %u %u %u %u",cpup->name,&cpup->user,&cpup->nice,&cpup->system,&cpup->idle);
				fclose(fd);
}

void cal_cpu_occupy(double *rate){
				unsigned long od,nd;
				unsigned long id,sd;
				double cpuuse=0.0;
				struct cpu_occupy cpuy1;
				struct cpu_occupy cpuy2;

				get_cpu_occupy((struct cpu_occupy *)&cpuy1);
				sleep(1);
				get_cpu_occupy((struct cpu_occupy *)&cpuy2);
				od=(unsigned long)(cpuy1.user+cpuy1.nice+cpuy1.system+cpuy1.idle);
				nd=(unsigned long)(cpuy2.user+cpuy2.nice+cpuy2.system+cpuy2.idle);
				id=(unsigned long)(cpuy2.user-cpuy1.user);
				sd=(unsigned long)(cpuy2.system-cpuy2.system);
				if((nd-od)!=0){
								cpuuse=(double)(sd+id)*100/(nd-od);
				}
				*rate=cpuuse;
}

void cal_mem_occupy(double *rate){
				if(system("cat /proc/meminfo|awk '{print $2}'>mem.info")!=-1){
								FILE *fd;
								char buf[64]={0};
								fd=fopen("mem.info","r");
								fgets(buf,sizeof(buf),fd);
								unsigned long total=atol(buf);
								fgets(buf,sizeof(buf),fd);
								fgets(buf,sizeof(buf),fd);
								fclose(fd);
								unsigned long available=atol(buf);
								*rate=(1.0-(double)available/total)*100;
				}else{
								*rate=0.0;
				}
}

void cal_net_info(unsigned long *rx,unsigned long *tx){
				if(system("cat /proc/net/dev|sed -n '3,1p'|awk '{print $2,$10}'>net.info")!=-1){
								FILE *fd;
								char *p;
								char buf[64]={0};
								fd=fopen("net.info","r");
								fgets(buf,sizeof(buf),fd);
								fclose(fd);
								p=strchr(buf,' ');
								*p='\0';
								*rx=atol(buf);
								*tx=atol(p+1);
				}else{
								*rx=0;*tx=0;
				}
}
